//
//  CoopenViewController.m
//  布局测试
//
//  Created by ZhangLin on 2019/3/20.
//  Copyright © 2019年 张翰林. All rights reserved.
//

#import "CoopenViewController.h"
#import <Masonry.h>
#import "ViewController.h"

#import "TabBarController.h"
#import "LoginViewController.h"
#import "NavigateViewController.h"
#import "GestureViewController.h"
#import "FingerprintUnlockViewController.h"

#import <UIImageView+WebCache.h>
#import "CoopenModelPack.h"

#define Window [UIApplication sharedApplication].keyWindow

@interface CoopenViewController ()

@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic,strong) UIButton *btn;
@property(nonatomic,assign)NSInteger seconds;

@property(nonatomic,strong)UIImageView *adImv;


@end

@implementation CoopenViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.seconds = 3;
    //开屏页View
    [self createView];
//    //开屏页显示
    self.adImv.image = [UIImage imageNamed:@"Splash_bg"];

    
}
-(void)createView
{
    self.adImv = [[UIImageView alloc] initWithFrame:self.view.bounds];
    //从缓存中取图片  如果没有用本地的
    
    [self.view addSubview:self.adImv];
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/home/openScreenPage"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOSTNOTips:url Parameters:nil HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            dispatch_async(dispatch_get_main_queue(), ^{
               [self dealshareProfitResponse:responseObject];
            });
            
        }
    } FailedHandler:^(NSError *error) {
        
        
        
    }];
}
-(void)dealshareProfitResponse:(id)responseObject
{
    CoopenResponse *coopenresponse = [CoopenResponse mj_objectWithKeyValues:responseObject];
    if ([coopenresponse.response.sudokuList count] > 0) {
        NSString *imageUrl = [coopenresponse.response.sudokuList[0] imagePath];
        //获取到图片，保存到缓存中
        self.adImv.image = [UIImage imageNamed:@"Splash_bg"];
//        [self.adImv sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"Splash_bg"]];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.seconds = 3;
    self.adImv = [[UIImageView alloc] initWithFrame:self.view.bounds];
    //从缓存中取图片  如果没有用本地的
    
    [self.view addSubview:self.adImv];
    //    //开屏页显示
    self.adImv.image = [UIImage imageNamed:@"Splash_bg"];
    
    [self createcountDown];
    __weak typeof(self) weakSelf = self;
    if (@available(iOS 10.0, *)) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
            [weakSelf timerRun];
        }];
    } else {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    }

}
-(void)checkSettings
{
    /*
    //赋值
    NSData *data1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"u"];
    UserInfo *u = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
    [AppTools shareInstance].userInfo = u;
     */
    
    //判断是否开启了指纹或者手势
    if ([AppTools isIsOpenedGesture] && [AppTools isOpenedFingerprint]) {      //指纹：开  手势：开
        GestureViewController *gesVC = [[GestureViewController alloc] init];
        [gesVC setType:(GestureViewControllerTypeLogin)];
        NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:gesVC];
        Window.rootViewController = nav;
    }else if (![AppTools isIsOpenedGesture] && [AppTools isOpenedFingerprint])  //指纹：开  手势：关
    {
        FingerprintUnlockViewController *fingerVC = [[FingerprintUnlockViewController alloc] init];
        NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:fingerVC];
        Window.rootViewController = nav;
    }else if ([AppTools isIsOpenedGesture] && ![AppTools isOpenedFingerprint])  //指纹：关  手势：开
    {
        GestureViewController *gesVC = [[GestureViewController alloc] init];
        [gesVC setType:(GestureViewControllerTypeLogin)];
        NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:gesVC];
        Window.rootViewController = nav;
    }else if (![AppTools isIsOpenedGesture] && ![AppTools isOpenedFingerprint]) //指纹：关  手势：关
    {
        //设置登录状态为登出 并清除token 存储的userInfo清空
        [AppTools saveLoginStatus:NO];
        [AppTools setToken:@""];
        [AppTools shareInstance].userInfo = [[UserInfo alloc] init];
        
        /*
        //把登录返回的数据模型存储到本地
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[AppTools shareInstance].userInfo];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"u"];
         */
        
        Window.rootViewController = [[TabBarController alloc]init];
    }
}

- (void)timerRun {
    NSLog(@"%s", __func__);
    self.seconds--;
    NSLog(@"%ld",(long)self.seconds);
    [self.btn setTitle:[NSString stringWithFormat:@"%ld%@",(long)self.seconds,@"S 跳过"] forState:(UIControlStateNormal)];
    
    if (self.seconds == 0) {
        //是否开启了指纹或者手势、跳转对应的页面
        if ([AppTools isLogin]) {
            [self checkSettings];
        }else
        {
            Window.rootViewController = [[TabBarController alloc]init];
        }
    }
    
    if (self.seconds < 0) {
        [self.timer invalidate];
    }
    
}

- (void)dealloc {
    [self.timer invalidate];
    NSLog(@"%s", __func__);
}

-(void)createcountDown
{
    self.btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.btn setTitle:@"3S 跳过" forState:(UIControlStateNormal)];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self.btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn bringSubviewToFront:self.adImv];
    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(10 + 20);
        make.height.mas_equalTo(20);
    }];
    [self.btn addTarget:self action:@selector(btnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
//点击跳过
-(void)btnAction
{
    NSLog(@"点击跳过");
    [self.timer invalidate];
    //是否开启了指纹或者手势、跳转对应的页面
    if ([AppTools isLogin]) {
        [self checkSettings];
    }else
    {
        Window.rootViewController = [[TabBarController alloc]init];
    }
    
}







@end
